/*
 * here we will test both the methods of Media class and the one specifics
 * to Book class
 * @author M.T. Segarra, G. Piraux, M-A. Taupenot
 * @version 1.0.1
 * date:21/05/2012 
 */
package tests;

import static org.junit.Assert.*;

import org.junit.*;

import java.util.*;

import subscribersManagement.Subscriber;
import loansManagement.Loan;
import booksManagement.Book;
//import booksManagement.DVD;
import booksManagement.Copy;
import booksManagement.Media;
import exceptions.*;

/**
 * @author        user
 */
public class BookTest {

	/**
   * @uml.property  name="s"
   * @uml.associationEnd  
   */
	private Subscriber s;
	
	private ArrayList<Media> medias = new ArrayList<Media>();
	/**
   * @uml.property  name="b"
   * @uml.associationEnd  
   */
	private Book b;
	/**
   * @uml.property  name="d"
   * @uml.associationEnd  
   */
	private Copy c1;
	/**
   * @uml.property  name="c2"
   * @uml.associationEnd  
   */
	private Copy c2;
	/**
   * @uml.property  name="authors"
   */
	private ArrayList<String> authors;
	/**
   * @uml.property  name="day"
   */
	private Calendar day;
	/**
   * @uml.property  name="Id"
   */
	private long Id;
	
	@Before
	public void beforeTests() throws BadParametersException {
		s = new Subscriber("Mayte", "Segarra", new GregorianCalendar());
		day = new GregorianCalendar();
		Id = 55555;
		authors = new ArrayList<String>();
		authors.add("Corral");
		b = new Book("Noche", authors, new GregorianCalendar(), "026765415");
		c1 = b.addCopy();
		
	}
	
	/*
	 * functions to test:
	 * 
	 * Media.java: Media(title,date),Media(title,date,copyAmount),getId(),setId(Id),
	 * getTitle(),setTitle(title),getEditionDate(),setEditionDate(date),getNbLentCopies(),
	 * setNbLentCopies(),noLentCopy(),allCopiesLent(),getCopies(),setCopies(),addCopy(),
	 * removeCopy(),toString()
	 * 
	 * Book.java: Book(title,authors,date,isbn),getAuthors(),setAuthors(),getIsbn(),
	 * setIsbn(),equals(book),toString()
	 */
	
	//test of Media class methods 
	
	@Test(expected = Exception.class)
	public void testMedia() throws BadParametersException {
		
	}
	
	@Test
	public void testEqualsBook() throws BadParametersException {
		Book b2 = new Book("Noche", authors, new GregorianCalendar(), "026765415");
		assertTrue(b.equals(b2));
		
		b2 = new Book("Noche", authors, new GregorianCalendar(), "020178517");
		assertFalse(b.equals(b2));
		
		b2 = new Book("El dia", authors, new GregorianCalendar(), "026765415");
		assertTrue(b.equals(b2));
	}

	@Test
	public void testOKBook() throws BadParametersException {
		System.out.println(medias+ "\n");
		Book b2 = new Book("Noche", authors, new GregorianCalendar(), "020178515");
		medias.add(b2);
		System.out.println(medias);
	}
	
	@Test
	public void testOKCopy() throws BadParametersException {
		System.out.println(medias+ "\n");
		Book b2 = new Book("Noche", authors, new GregorianCalendar(), "020178515");
		medias.add(b2);
		b2.addCopy();
		b2.addCopy();
		System.out.println(b2.getCopies());
	}
	
	@Test(expected = BadParametersException.class)
	public void testNullBook() throws BadParametersException{
		new Book("Noche", authors, new GregorianCalendar(), null);
		authors = null;
		new Book(null, authors, new GregorianCalendar(), "020178515");
		new Book("Noche", authors, null, "020178515");
		new Book("Noche", authors, new GregorianCalendar(), null);
	}
	
	@Test
	public void testLendBook() throws BadParametersException, 
	TooManyLoansException, LentBookException, ReservedCopyException{
		System.out.println("nombre d'emprunts : " + s.getCurrentLoans().size());
		Loan l = new Loan(c1,s);
		assertTrue(c1.getCopyLoan() != null);
	System.out.println("abonné : " + l.getLender());
	System.out.println("nombre d'emprunts : " + l.getLender().getCurrentLoans().size());
	}
	
	@Test(expected = LentBookException.class)
	public void testLendLentBook() throws BadParametersException, 
	TooManyLoansException, LentBookException, ReservedCopyException{
		new Loan(c1,s);
		//s = new Subscriber("Mayte", "Seg", new GregorianCalendar());
		new Loan(c1,s);
		
	}
	
	@Test
	public void testReturnBook() throws BadParametersException, 
	TooManyLoansException, LentBookException, ReservedCopyException{
		new Loan(c1,s);
		System.out.println(s.getCurrentLoans().size());
		c1.returnCopy();
		assertTrue(c1.getCopyLoan() == null);
		System.out.println(s.getCurrentLoans().size());
	}
	
	@Test
	public void testAddCopy() throws BadParametersException {
		Book b2 = new Book("Noche", authors, new GregorianCalendar(), "026765415");
		System.out.println("Nombre d'exemplaires : " + b2.getCopies().size());
		b2.addCopy();
		assertTrue(b2.getCopies().size() == 1);
		System.out.println("Nombre d'exemplaires : " + b2.getCopies().size());
	}
}
